//
//  MKUICollectionViewModel+MKUICollectionViewDelegatePageFlowLayout.m
//  MKUI
//
//  Created by 苏尚进 on 2021/12/29.
//  Copyright © 2021 moon. All rights reserved.
//

#import "MKUICollectionViewModel+MKUICollectionViewDelegatePageFlowLayout.h"

@implementation MKUICollectionViewModel (MKUICollectionViewDelegatePageFlowLayout)
- (CGSize)collectionView:(UICollectionView *)collectionView pageFlowLayout:(MKUICollectionViewPageFlowLayout *)collectionViewLayout itemSizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    id<MKUICollectionViewDelegatePageFlowLayout> forwardDelegate = nil;
    if([self.forwardDelegate conformsToProtocol:@protocol(MKUICollectionViewDelegatePageFlowLayout)]){
        forwardDelegate = (id<MKUICollectionViewDelegatePageFlowLayout>)self.forwardDelegate;
    }
    CGRect bounds = collectionView.bounds;
    CGSize size = CGSizeZero;
    if([collectionViewLayout isKindOfClass:[MKUICollectionViewPageFlowLayout class]]){
        MKUICollectionViewPageFlowLayout *flowlayout = (MKUICollectionViewPageFlowLayout *)collectionViewLayout;
        if(CGSizeEqualToSize(bounds.size,CGSizeZero)){//flowlayout布局时,0尺寸会出现"the item height must be less that the height of the UICollectionView minus the section insets top and bottom values."的警告,因此直接设置0的itemsize
            size = CGSizeZero;
        }else{
            size = flowlayout.itemSize;
        }
    }
    if([forwardDelegate respondsToSelector:_cmd]){
        size = [forwardDelegate collectionView:collectionView pageFlowLayout:collectionViewLayout itemSizeForItemAtIndexPath:indexPath];
    }else{
        MKUICollectionViewCellModel *cm = [self cellModelAtIndexPath:indexPath];
        Class cellClass = cm.cellClass;
        if([cellClass respondsToSelector:@selector(sizeWithCollectionView:collectionCellModel:)]){
            size = [cellClass sizeWithCollectionView:collectionView collectionCellModel:cm];
        }
    }
    return size;
}
@end
